package com.woniu.woniuk16.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.woniuk16.commens.ComUtil;
import com.woniu.woniuk16.entity.WoniuStudentScore;
import com.woniu.woniuk16.exception.stu.CourceExceprion;
import com.woniu.woniuk16.mapper.WoniuStudentScoreMapper;
import com.woniu.woniuk16.param.AddScore;
import com.woniu.woniuk16.service.IWoniuStudentScoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author tiangle
 * @since 2021-12-26
 */
@Service
public class WoniuStudentScoreServiceImpl extends ServiceImpl<WoniuStudentScoreMapper, WoniuStudentScore> implements IWoniuStudentScoreService {

    @Autowired
    private RedisTemplate<String,Object> rtl;

    @Override
    public boolean addOne(AddScore addScore) throws CourceExceprion {
        if (addScore.getScoreScore()==null)
            throw new CourceExceprion("分数未填写");
        if (addScore.getScoreScore()>100||addScore.getScoreScore()<0){
            throw new CourceExceprion("分数不对");
        }
        if (addScore.getStudentId()==null){
            throw new CourceExceprion("先选择学生");
        }
        Set<Object> range = rtl.opsForZSet().reverseRange("woniu:course:score:" + addScore.getClassUserId(), 0, -1);
        for (Object o : range) {
            if (((AddScore)o).getStudentId()==addScore.getStudentId()) {
                throw new CourceExceprion("该同学已经存在成绩");
            }
        }


        rtl.opsForZSet().add("woniu:course:score:"+addScore.getClassUserId(),addScore,addScore.getScoreScore());
        return true;
    }

    @Override
    public Object getOne(Integer id) throws CourceExceprion {
        Set<Object> range = rtl.opsForZSet().reverseRange("woniu:course:score:" + id, 0, -1);
        if (range==null||range.size()<=0){
            QueryWrapper<WoniuStudentScore> qw = new QueryWrapper<>();
            qw.eq("class_user_id",id);
            List<WoniuStudentScore> list = list(qw);
            if (list==null||list.size()<=0){
                throw new CourceExceprion("该课还没有添加学员分数");
            }
//            rtl.opsForZSet().add("woniu:course:score:" + id,);
//            添加到redis中

            return list;
        }
        return range;
    }

    public boolean delAll(Integer id){

        return rtl.delete("woniu:course:score:" + id);
    }

    @Override
    public boolean submit(Integer id) throws CourceExceprion {
        Set<Object> range = rtl.opsForZSet().reverseRange("woniu:course:score:" + id, 0, -1);
        if (range==null||range.size()<=0){
            throw new CourceExceprion("还未添加数据");
        }
        QueryWrapper<WoniuStudentScore> qw = new QueryWrapper<>();
        qw.eq("class_user_id",id);
        remove(qw);
        ArrayList<WoniuStudentScore> al = new ArrayList<>();
        for (Object o : range) {
            al.add(ComUtil.copyProperties((AddScore) o,WoniuStudentScore.class));
        }
        List<WoniuStudentScore> woniuStudentScores = ComUtil.copyList(al, WoniuStudentScore.class);
        saveBatch(woniuStudentScores);
        return true;
    }
}
